#include<bits/stdc++.h>
using namespace std;

void panduan(string cha,string str){
  int m=0;
  int found_first,found_end,count=1e6;
  for(int i=0;i<str.length();i++){
    if(!binary_search(cha.begin(),cha.end(),str[i])){
      cha[m++]=str[i];
      found_first=i;
      if(!cha[26]==0){
        found_end=i;
        if(count>found_end-found_first)count=found_end-found_first;
        for(int j=0;i<27;j++){
          cha[j]=0;
        }
        if(!i==str.length()-1)panduan(cha,str);
      }
    }
  }
  cout<<count<<endl;
}

int main(){
  int T;
  cin>>T;
  char cha[27];
  memset(cha,0,sizeof(cha));
  string str;
  while(T--){
    cin>>str;
    panduan(cha,str);
  }

  return 0;
}